﻿//+--------------------------------------------------------------------------------------------------
//     
//Description: DefinitionTreeItemMenuCommandRouter
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------


using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using VmroleAuthor.Commands;
using VmroleAuthor.Models;
using VmroleAuthor.Views;

namespace VmroleAuthor.ViewModels
{
    public class DefinitionTreeItemMenuCommandRouter
    {
        private readonly DefinitionViewModel definitionViewModel;

        public DefinitionTreeItemMenuCommandRouter(DefinitionViewModel definitionViewModel)
        {
            this.definitionViewModel = definitionViewModel;

            this.AddWebDeployCommand = new RelayCommand(
             param => this.AddWebDeployApplication((TreeViewItemViewModel)param),
             param => this.CanAddWebDeployApplication((TreeViewItemViewModel)param));

            this.AddScriptCommandCommand = new RelayCommand(
                 param => this.AddScriptCommand((TreeViewItemViewModel)param),
                param => this.CanAddScriptCommand((TreeViewItemViewModel)param));

            this.AddProvisioningScriptCommand = new RelayCommand(
                param => this.AddProvisioningScript((TreeViewItemViewModel)param),
                param => this.CanAddProvisioningScript((TreeViewItemViewModel)param));

            this.AddScriptAppCommand = new RelayCommand(
               param => this.AddScriptApplication((TreeViewItemViewModel)param),
               param => this.CanAddScriptApplication((TreeViewItemViewModel)param));

            this.AddSqldacCommand = new RelayCommand(
               param => this.AddSqldacApplication((TreeViewItemViewModel)param),
               param => this.CanAddSqldacApplication((TreeViewItemViewModel)param));

            this.AddSqlScriptCommandCommand = new RelayCommand(
                 param => this.AddSqlScriptCommand((TreeViewItemViewModel)param),
                param => this.CanAddSqlScriptCommand((TreeViewItemViewModel)param));

            this.AddSqlProfileCommand = new RelayCommand(
               param => this.AddSqlProfile((TreeViewItemViewModel)param),
               param => this.CanAddSqlProfile((TreeViewItemViewModel)param));

            this.AddSqlDeploymentCommand = new RelayCommand(
               param => this.AddSqlDeployment((TreeViewItemViewModel)param),
               param => this.CanAddSqlDeployment((TreeViewItemViewModel)param));

            this.AddNetworkAdapterCommand = new RelayCommand(
                    param => this.AddNetworkAdapter((TreeViewItemViewModel)param),
                    param => this.CanAddNetworkAdapter((TreeViewItemViewModel)param));

            this.AddViewDefSectionCommand = new RelayCommand(
             param => this.AddViewDefSection((TreeViewItemViewModel)param),
             param => this.CanAddViewDefSection((TreeViewItemViewModel)param));

            this.AddViewDefCategoryCommand = new RelayCommand(
               param => this.AddViewDefCategory((TreeViewItemViewModel)param),
               param => this.CanAddViewDefCategory((TreeViewItemViewModel)param));


            this.RemoveTreeItemCommand = new RelayCommand(
               param => this.RemoveTreeItem((TreeViewItemViewModel)param),
               param => this.CanRemoveTreeItem((TreeViewItemViewModel)param));

            this.SaveAsCommand = new RelayCommand(
               param => this.SaveAsTreeItem((TreeViewItemViewModel)param),
               param => this.CanSaveAsTreeItem((TreeViewItemViewModel)param));

            this.ViewWorkingDirectoryCommand = new RelayCommand(
               param => this.ViewWorkingDirectory((TreeViewItemViewModel)param),
               param => this.CanViewWorkingDirectory((TreeViewItemViewModel)param));

            this.DeployResExtToAzureCommand = new RelayCommand(
               param => this.DeployResExtToAzure((TreeViewItemViewModel)param),
               param => this.CanDeployResExtToAzure((TreeViewItemViewModel)param));
        }

        public ICommand ViewWorkingDirectoryCommand { get; private set; }

        public bool CanViewWorkingDirectory(TreeViewItemViewModel treeItemViewModel)
        {
            return true;
        }

        public void ViewWorkingDirectory(TreeViewItemViewModel treeItemViewModel)
        {
            string workingDir = null;

            while (treeItemViewModel != null)
            {
                VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel = treeItemViewModel as VmRoleResourceExtensionViewModel;
                if ((vmRoleResourceExtensionViewModel != null))
                {
                    Debug.Assert(vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo != null);
                    workingDir = vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir;
                    break;
                }
                VmRoleResourceDefinitionViewModel vmRoleResourceDefinitionViewModel = treeItemViewModel as VmRoleResourceDefinitionViewModel;
                if (vmRoleResourceDefinitionViewModel != null)
                {
                    Debug.Assert(vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo != null);
                    workingDir = vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.WorkingDir;
                    break;
                }
                treeItemViewModel = treeItemViewModel.Parent;
            }

            if (treeItemViewModel == null || workingDir == null)
            {
                return;
            }
            Process.Start(workingDir);
        }


        public ICommand SaveAsCommand { get; private set; }

        public bool CanSaveAsTreeItem(TreeViewItemViewModel treeItemViewModel)
        {
            return true;
        }

        public void SaveAsTreeItem(TreeViewItemViewModel treeItemViewModel)
        {
            string prevSaveAsPath = null, prevWorkingDir = null, prevJsonFileName = null, itemName = null;
            VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel = null;
            VmRoleResourceDefinitionViewModel vmRoleResourceDefinitionViewModel = null;

            while (treeItemViewModel != null)
            {
                vmRoleResourceExtensionViewModel = treeItemViewModel as VmRoleResourceExtensionViewModel;
                if ((vmRoleResourceExtensionViewModel != null))
                {
                    Debug.Assert(vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo != null);
                    prevSaveAsPath = vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.FileToSavePath;
                    prevWorkingDir = vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir;
                    prevJsonFileName = vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.JsonFileName;
                    itemName = vmRoleResourceExtensionViewModel.Name;
                    break;
                }
                vmRoleResourceDefinitionViewModel = treeItemViewModel as VmRoleResourceDefinitionViewModel;
                if (vmRoleResourceDefinitionViewModel != null)
                {
                    Debug.Assert(vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo != null);
                    prevSaveAsPath = vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.FileToSavePath;
                    prevWorkingDir = vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.WorkingDir;
                    prevJsonFileName = vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.JsonFileName;
                    itemName = vmRoleResourceDefinitionViewModel.Name;
                    break;
                }
                treeItemViewModel = treeItemViewModel.Parent;
            }
            if (treeItemViewModel == null || (vmRoleResourceExtensionViewModel == null && vmRoleResourceDefinitionViewModel == null))
            {
                return;
            }


            var sfw = new SaveAsFileWindow(prevSaveAsPath, prevWorkingDir);
            Window window = new Window
            {
                Title = "Save  As",
                Content = sfw,
                Name = SaveAsFileWindow.SaveAsFileWindowName,
                Height = 270,
                Width = 700
            };
            window.ShowDialog();
            if (sfw.DataValid)
            {
                try
                {
                    bool copyResExtPkgWorkingDir = vmRoleResourceExtensionViewModel != null && vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo != null;
                    bool copyResDefPkgWorkingDir = vmRoleResourceDefinitionViewModel != null && vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.IsResdefPkg;
                    bool copy = copyResDefPkgWorkingDir || copyResExtPkgWorkingDir;
                    if (copy && !string.Equals(prevWorkingDir, sfw.WorkingDir.Text, StringComparison.OrdinalIgnoreCase))
                    {
                        ReferencedApplicationPayloadViewModel.DirectoryCopy(prevWorkingDir, sfw.WorkingDir.Text);
                    }

                    string prevJsonFileNameInsidePackage = System.IO.Path.GetFileName(prevJsonFileName);
                    Debug.Assert(prevJsonFileNameInsidePackage != null);
                    string newJsonFileNameInsidePackage = System.IO.Path.Combine(sfw.WorkingDir.Text, prevJsonFileNameInsidePackage);
                    if (vmRoleResourceExtensionViewModel != null)
                    {
                        vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir = sfw.WorkingDir.Text;
                        vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.FileToSavePath = sfw.FileLocation.Text;
                        vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.JsonFileName = newJsonFileNameInsidePackage;
                        vmRoleResourceExtensionViewModel.SaveToDisk();
                    }
                    else
                    {
                        vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.FileToSavePath = sfw.FileLocation.Text;
                        vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.WorkingDir = sfw.WorkingDir.Text;
                        if (vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.IsResdefPkg)
                        {
                            vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.JsonFileName = newJsonFileNameInsidePackage;
                        }
                        else
                        {
                            vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.JsonFileName = sfw.FileLocation.Text;
                        }

                        if (vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.SmallIconFileName != null)
                        {
                            string prevIconFileNameInsidePackage = System.IO.Path.GetFileName(vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.SmallIconFileName);
                            string newIconFileNameInsidePackage = System.IO.Path.Combine(sfw.WorkingDir.Text, prevIconFileNameInsidePackage ?? string.Empty);
                            vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.SmallIconFileName = newIconFileNameInsidePackage;
                        }
                        if (vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.MediumIconFileName != null)
                        {
                            string prevIconFileNameInsidePackage = System.IO.Path.GetFileName(vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.MediumIconFileName);
                            string newIconFileNameInsidePackage = System.IO.Path.Combine(sfw.WorkingDir.Text, prevIconFileNameInsidePackage ?? string.Empty);
                            vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.MediumIconFileName = newIconFileNameInsidePackage;
                        }
                        if (vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.LargeIconFileName != null)
                        {
                            string prevIconFileNameInsidePackage = System.IO.Path.GetFileName(vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.LargeIconFileName);
                            string newIconFileNameInsidePackage = System.IO.Path.Combine(sfw.WorkingDir.Text, prevIconFileNameInsidePackage ?? string.Empty);
                            vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.LargeIconFileName = newIconFileNameInsidePackage;
                        }
                        if (vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.ResourceFileNames != null)
                        {
                            for (int i = 0; i < vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.ResourceFileNames.Count; ++i)
                            {
                                string prevResFileInsidePackage = System.IO.Path.GetFileName(vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.ResourceFileNames[i]);
                                string newResFileNameInsidePackage = System.IO.Path.Combine(sfw.WorkingDir.Text, prevResFileInsidePackage ?? string.Empty);
                                vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.ResourceFileNames[i] = newResFileNameInsidePackage;
                            }
                        }
                        vmRoleResourceDefinitionViewModel.SaveToDisk();
                    }
                }
                catch (Exception ex)
                {
                    string fmtError = string.Format(
                            "Exception While saving the item {0} to new path {1} with new working dir {2}. Error {3}",
                            itemName ?? string.Empty, sfw.WorkingDir.Text, sfw.FileLocation.Text, ex.Message);
                    MessageBox.Show(fmtError, DefinitionViewModel.ToolHeader);
                    if (vmRoleResourceExtensionViewModel != null)
                    {
                        vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.WorkingDir = prevWorkingDir;
                        vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.FileToSavePath = prevSaveAsPath;
                        vmRoleResourceExtensionViewModel.ResourceExtensionStorageInfo.JsonFileName = prevJsonFileName;
                    }
                    else
                    {
                        vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.FileToSavePath = prevSaveAsPath;
                        vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.WorkingDir = prevWorkingDir;
                        vmRoleResourceDefinitionViewModel.ResourceDefinitionStorageInfo.JsonFileName = prevJsonFileName;
                    }
                }
            }
        }


        public ICommand RemoveTreeItemCommand { get; private set; }

        public bool CanRemoveTreeItem(TreeViewItemViewModel treeItemViewModel)
        {
            if (treeItemViewModel == null)
            {
                return false;
            }
            ICanBeRemovedFromTree canBeRemovedFromTree = treeItemViewModel as ICanBeRemovedFromTree;
            if (canBeRemovedFromTree == null)
            {
                return false;
            }
            TreeViewItemViewModel parentItemViewModel = treeItemViewModel.Parent;
            if (parentItemViewModel == null)
            {
                //parent is root. TODO: revise this with viewdefs
                return true;
            }
            IRemoveChildItem removeChildItem = parentItemViewModel as IRemoveChildItem;
            return removeChildItem != null;
        }

        public void RemoveTreeItem(TreeViewItemViewModel treeItemViewModel)
        {
            if (treeItemViewModel == null)
            {
                return;
            }
            TreeViewItemViewModel parentItemViewModel = treeItemViewModel.Parent;
            if (parentItemViewModel == null)
            {
                //parent is root. TODO: revise this with viewdefs
                this.definitionViewModel.RemoveChild(treeItemViewModel);
            }
            else
            {
                IRemoveChildItem removeChildItem = parentItemViewModel as IRemoveChildItem;
                if (removeChildItem != null)
                {
                    removeChildItem.RemoveChild(treeItemViewModel);
                }
            }
        }

        public ICommand AddWebDeployCommand
        {
            get;
            private set;
        }

        public bool CanAddWebDeployApplication(TreeViewItemViewModel parentItemViewModel)
        {
            IAddWebDeployApplication addWebDeployApplication = parentItemViewModel as IAddWebDeployApplication;
            return addWebDeployApplication != null && addWebDeployApplication.CanAddWebDeployApplication;
        }

        public void AddWebDeployApplication(TreeViewItemViewModel parentItemViewModel)
        {
            IAddWebDeployApplication addWebDeployApplication = parentItemViewModel as IAddWebDeployApplication;
            if (addWebDeployApplication != null)
            {
                addWebDeployApplication.AddWebDeployApplication();
            }
        }

        public ICommand AddScriptCommandCommand
        {
            get;
            private set;
        }

        public bool CanAddScriptCommand(TreeViewItemViewModel parentItemViewModel)
        {
            IAddScriptCommand addScriptCommand = parentItemViewModel as IAddScriptCommand;
            return addScriptCommand != null && addScriptCommand.CanAddScriptCommand;
        }

        public void AddScriptCommand(TreeViewItemViewModel parentItemViewModel)
        {
            IAddScriptCommand addScriptCommand = parentItemViewModel as IAddScriptCommand;
            if (addScriptCommand != null)
            {
                addScriptCommand.AddScriptCommand();
            }
        }

        public ICommand AddProvisioningScriptCommand
        {
            get;
            private set;
        }

        public bool CanAddProvisioningScript(TreeViewItemViewModel parentItemViewModel)
        {
            IAddProvisioningScript addProvisioningScript = parentItemViewModel as IAddProvisioningScript;
            return addProvisioningScript != null && addProvisioningScript.CanAddProvisioningScript;
        }

        public void AddProvisioningScript(TreeViewItemViewModel parentItemViewModel)
        {
            IAddProvisioningScript addProvisioningScript = parentItemViewModel as IAddProvisioningScript;
            if (addProvisioningScript != null)
            {
                addProvisioningScript.AddProvisioningScript();
            }
        }

        public ICommand AddScriptAppCommand
        {
            get;
            private set;
        }

        public bool CanAddScriptApplication(TreeViewItemViewModel parentItemViewModel)
        {
            IAddScriptApplication addScriptApplication = parentItemViewModel as IAddScriptApplication;
            return addScriptApplication != null && addScriptApplication.CanAddScriptApplication;
        }

        public void AddScriptApplication(TreeViewItemViewModel parentItemViewModel)
        {
            IAddScriptApplication addScriptApplication = parentItemViewModel as IAddScriptApplication;
            if (addScriptApplication != null)
            {
                addScriptApplication.AddScriptApplication();
            }
        }

        public ICommand AddSqldacCommand
        {
            get;
            private set;
        }

        public bool CanAddSqldacApplication(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqldacApplication addSqldacApplication = parentItemViewModel as IAddSqldacApplication;
            return addSqldacApplication != null && addSqldacApplication.CanAddSqldacApplication;
        }

        public void AddSqldacApplication(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqldacApplication addSqldacApplication = parentItemViewModel as IAddSqldacApplication;
            if (addSqldacApplication != null)
            {
                addSqldacApplication.AddSqldacApplication();
            }
        }

        public ICommand AddSqlScriptCommandCommand
        {
            get;
            private set;
        }

        public bool CanAddSqlScriptCommand(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqlScriptCommand addSqlScriptCommand = parentItemViewModel as IAddSqlScriptCommand;
            return addSqlScriptCommand != null && addSqlScriptCommand.CanAddSqlScriptCommand;
        }

        public void AddSqlScriptCommand(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqlScriptCommand addSqlScriptCommand = parentItemViewModel as IAddSqlScriptCommand;
            if (addSqlScriptCommand != null)
            {
                addSqlScriptCommand.AddSqlScriptCommand();
            }
        }

        public ICommand AddSqlProfileCommand
        {
            get;
            private set;
        }

        public bool CanAddSqlProfile(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqlProfile addSqlProfile = parentItemViewModel as IAddSqlProfile;
            return addSqlProfile != null && addSqlProfile.CanAddSqlProfile;
        }

        public void AddSqlProfile(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqlProfile addSqlProfile = parentItemViewModel as IAddSqlProfile;
            if (addSqlProfile != null)
            {
                addSqlProfile.AddSqlProfile();
            }
        }

        public ICommand AddSqlDeploymentCommand
        {
            get;
            private set;
        }

        public bool CanAddSqlDeployment(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqlDeployment addSqlDeployment = parentItemViewModel as IAddSqlDeployment;
            return addSqlDeployment != null && addSqlDeployment.CanAddSqlDeployment;
        }

        public void AddSqlDeployment(TreeViewItemViewModel parentItemViewModel)
        {
            IAddSqlDeployment addSqlDeployment = parentItemViewModel as IAddSqlDeployment;
            if (addSqlDeployment != null)
            {
                addSqlDeployment.AddSqlDeployment();
            }
        }

        public ICommand AddNetworkAdapterCommand
        {
            get;
            private set;
        }

        public bool CanAddNetworkAdapter(TreeViewItemViewModel parentItemViewModel)
        {
            IAddNetworkAdapter addNetworkAdapter = parentItemViewModel as IAddNetworkAdapter;
            return addNetworkAdapter != null && addNetworkAdapter.CanAddNetworkAdapter;
        }

        public void AddNetworkAdapter(TreeViewItemViewModel parentItemViewModel)
        {
            IAddNetworkAdapter addNetworkAdapter = parentItemViewModel as IAddNetworkAdapter;
            if (addNetworkAdapter != null)
            {
                addNetworkAdapter.AddNetworkAdapter();
            }
        }

        public ICommand AddViewDefSectionCommand
        {
            get;
            private set;
        }

        public bool CanAddViewDefSection(TreeViewItemViewModel parentItemViewModel)
        {
            IAddViewDefSection addViewDefSection = parentItemViewModel as IAddViewDefSection;
            return addViewDefSection != null && addViewDefSection.CanAddViewDefSection;
        }

        public void AddViewDefSection(TreeViewItemViewModel parentItemViewModel)
        {
            IAddViewDefSection addViewDefSection = parentItemViewModel as IAddViewDefSection;
            if (addViewDefSection != null)
            {
                addViewDefSection.AddViewDefSection();
            }
        }

        public ICommand AddViewDefCategoryCommand
        {
            get;
            private set;
        }

        public bool CanAddViewDefCategory(TreeViewItemViewModel parentItemViewModel)
        {
            IAddViewDefCategory addViewDefCategory = parentItemViewModel as IAddViewDefCategory;
            return addViewDefCategory != null && addViewDefCategory.CanAddViewDefCategory;
        }

        public void AddViewDefCategory(TreeViewItemViewModel parentItemViewModel)
        {
            IAddViewDefCategory addViewDefCategory = parentItemViewModel as IAddViewDefCategory;
            if (addViewDefCategory != null)
            {
                addViewDefCategory.AddViewDefCategory();
            }
        }


        public ICommand DeployResExtToAzureCommand
        {
            get;
            private set;
        }


        public bool CanDeployResExtToAzure(TreeViewItemViewModel treeItemViewModel)
        {
            TreeViewItemViewModel applicableTreeItemViewModel = treeItemViewModel;
            while (applicableTreeItemViewModel != null && !(applicableTreeItemViewModel is VmRoleResourceExtensionViewModel))
            {
                applicableTreeItemViewModel = applicableTreeItemViewModel.Parent;
            }
            return (applicableTreeItemViewModel is VmRoleResourceExtensionViewModel);
        }

        public void DeployResExtToAzure(TreeViewItemViewModel treeItemViewModel)
        {
            TreeViewItemViewModel applicableTreeItemViewModel = treeItemViewModel;
            while (applicableTreeItemViewModel != null && !(applicableTreeItemViewModel is VmRoleResourceExtensionViewModel))
            {
                applicableTreeItemViewModel = applicableTreeItemViewModel.Parent;
            }
            VmRoleResourceExtensionViewModel resourceExtensionViewModel = applicableTreeItemViewModel as VmRoleResourceExtensionViewModel;
            if (resourceExtensionViewModel != null)
            {
                if (resourceExtensionViewModel.ResourceExtensionDefinition != null &&
                    resourceExtensionViewModel.ResourceExtensionDefinition.ExtensionSettings != null &&
                    resourceExtensionViewModel.ResourceExtensionDefinition.ExtensionSettings.ApplicationProfile != null &&
                    resourceExtensionViewModel.ResourceExtensionDefinition.ExtensionSettings.ApplicationProfile.LinuxApplicationProfile != null)
                {
                    string fmtError = string.Format(
                       "Currently only Windows Resource Extensions are supported for Microsoft Azure deployment.");
                    MessageBox.Show(fmtError, DefinitionViewModel.ToolHeader, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                // this is a windows resext
                this.definitionViewModel.ValidateSelectedResourceExtensionTreeViewItem(resourceExtensionViewModel);
                if (this.definitionViewModel.ValidationResults.Count > 0)
                {
                    string fmtError = string.Format(
                        "There are one or more errors detected in the selected resource extension {0}:{1}:{2}. They must be corrected before deployment can continue.",
                        resourceExtensionViewModel.Name ?? string.Empty,
                        resourceExtensionViewModel.Publisher ?? string.Empty,
                        resourceExtensionViewModel.Version ?? string.Empty);
                    MessageBox.Show(fmtError, DefinitionViewModel.ToolHeader,MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                else
                {
                    Window window = new DeployWindow(resourceExtensionViewModel);
                    window.ShowDialog();
                }
            }
        }

    }
}
